package cn.tedu.charging.order.Dao.repository.impl;

import cn.tedu.charging.order.Dao.mapper.OrderFailMapper;
import cn.tedu.charging.order.Dao.mapper.OrderSuccessMapper;
import cn.tedu.charging.order.Dao.repository.OrderRepository;
import cn.tedu.charging.order.common.OrderStatusConstants;
import cn.tedu.charging.order.pojo.po.ChargingOrderFailPO;
import cn.tedu.charging.order.pojo.po.ChargingOrderSuccessPO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class OrderRepositoryImpl implements OrderRepository {
    @Autowired
    private OrderSuccessMapper orderSuccessMapper;

    @Autowired
    private OrderFailMapper orderFailMapper;


    @Override
    public ChargingOrderSuccessPO getSuccessOrderByOrderNo(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id",orderNo);
        return orderSuccessMapper.selectOne(queryWrapper);
    }

    @Override
    public ChargingOrderFailPO getFailOrderByOrderNo(String orderNo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("bill_id",orderNo);
        return orderFailMapper.selectOne(queryWrapper);
    }

    @Override
    public int insertFailOrder(ChargingOrderFailPO chargingOrderFailPO) {
        return orderFailMapper.insert(chargingOrderFailPO);
    }

    @Override
    public int updateOrderStatusFromChargingToExceptionEnd(String orderNo) {
        //根据订单号查询订单状态
        //更新的目标值，要放到成功订单中
        ChargingOrderSuccessPO chargingOrderSuccessPO=new ChargingOrderSuccessPO();
        chargingOrderSuccessPO.setBillStatus(OrderStatusConstants.ORDER_STATUS_EXCEPTION_END);
        //UpdateWrapper是mybatisplus提供的一个封装类，用于封装更新条件
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("bill_id",orderNo);
        updateWrapper.eq("bill_status",OrderStatusConstants.ORDER_STATUS_CHARGING);
        return orderSuccessMapper.update(chargingOrderSuccessPO,updateWrapper);
    }
}
